package com.example.boot3.controller;


import com.alibaba.excel.EasyExcel;
import com.example.boot3.model.Repair;
import com.example.boot3.service.RepairService;
import com.example.boot3.util.JsonData;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.alibaba.excel.EasyExcelFactory;
import java.io.IOException;
import java.util.List;

/**
 * @author 邹成明
 */
@CrossOrigin(origins =  "*")
@RestController

public class RepairController {
    @Autowired
    private RepairService repairService;
    @GetMapping("repair_info")
    public JsonData getRepairList(){
        List<Repair> repairList  = repairService.getRepairList();
        return JsonData.buildSuccess(repairList);
    }

    @PostMapping("apply_repair")
    public JsonData applyRepair(@RequestBody Repair repair){
        Integer result = repairService.applyRepair(repair);
        if(result!=1){
            String msg="";
            if(result==2){
                msg=repair.getVehicle_id()+"正在出车";
            }
            return JsonData.buildError("");
        }
        else{
            return JsonData.buildSuccess(null);
        }
    }

    @GetMapping("repair/export")
    public void Export(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition", "attachment; filename=repairs.xlsx");
        List<Repair> data = repairService.getRepairList();
        EasyExcel.write(response.getOutputStream(), Repair.class).sheet("维修信息").doWrite(data);
    }

    @GetMapping("repair/cost/time")
    public JsonData getCostByTime(HttpServletResponse response) throws IOException {
       return JsonData.buildSuccess(repairService.getCostByTime());
    }


}
